using B.S.NewMedical.Api.Read.Application.Command.Department;
using B.S.NewMedical.Api.Read.DTOS.Department;
using B.S.NewMedical.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace B.S.NewMedical.Api.Read.Controllers.Department
{
    /// <summary>
    /// 科室查询控制器
    /// 提供科室信息的查询API接口
    /// 包含详细的日志记录和异常处理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DepartmentController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<DepartmentController> _logger;

        /// <summary>
        /// 构造函数
        /// 注入依赖的服务
        /// </summary>
        /// <param name="mediator">中介者模式，用于处理命令</param>
        /// <param name="logger">日志记录器</param>
        public DepartmentController(IMediator mediator, ILogger<DepartmentController> logger)
        {
            _mediator = mediator;
            _logger = logger;
            
            _logger.LogInformation("科室查询控制器初始化完成");
        }

        /// <summary>
        /// 获取科室详情
        /// 根据科室ID获取科室的详细信息
        /// </summary>
        /// <param name="id">科室ID</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>科室详细信息</returns>
        [HttpGet]
        public async Task<ApiResult<DepartmentDetailDto>> GetDepartmentDetail(int id, CancellationToken cancellationToken)
        {
            try
            {
                // 验证请求参数
                if (id <= 0)
                {
                    _logger.LogWarning("科室ID无效，请求被拒绝");
                    return new ApiResult<DepartmentDetailDto>
                    {
                        Code = ApiEnum.Fail,
                        Msg = "科室ID无效"
                    };
                }

                // 创建查询命令
                var command = new GetDepartmentDetailCommand { Id = id };
                
                // 执行查询操作
                var result = await _mediator.Send(command, cancellationToken);
                
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取科室详情时发生异常，科室ID: {DepartmentId}", id);
                return new ApiResult<DepartmentDetailDto>
                {
                    Code = ApiEnum.Error,
                    Msg = $"获取科室详情失败: {ex.Message}"
                };
            }
        }

                    /// <summary>
        /// 获取科室列表
        /// 获取树形结构的科室列表
        /// </summary>
        /// <param name="request">获取科室列表请求</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>树形结构的科室列表</returns>
        [HttpGet]
        public async Task<ApiResult<DepartmentListDto>> GetDepartmentList([FromQuery] GetDepartmentListCommand request, CancellationToken cancellationToken)
        {
            try
            {
                _logger.LogInformation("开始获取树形科室列表");
                
                // 执行查询操作
                var result = await _mediator.Send(request, cancellationToken);
                
                _logger.LogInformation("获取树形科室列表成功，总记录数: {TotalCount}", 
                    result.Data?.TotalCount ?? 0);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取树形科室列表时发生异常");
                return new ApiResult<DepartmentListDto>
                {
                    Code = ApiEnum.Error,
                    Msg = $"获取树形科室列表失败: {ex.Message}"
                };
            }
        }
    }
} 